#include <stdio.h>


// Beginning of Ex 4.9

void ex4_9_method_1()
{
    char str_1[400], str_2[400];
    int i = 0;
    int equal = 1;

    printf("Input str_1: \n");
    scanf("%s", str_1);
    getchar();

    printf("Input str_2: \n");
    scanf("%s", str_2);
    getchar();

    while(str_1[i] != '\0' || str_2[i] != '\0')
    {
        if(str_1[i] != str_2[i])
        {
            equal = 0;
            break;
        }
        i ++;
    }

    if(equal)
        printf("Equal");
    else
        printf("Not Equal");
}

void ex4_9_method_2()
{
    char str_1[400], str_2[400];
    int i = 0;

    printf("Input str_1: \n");
    scanf("%s", str_1);
    getchar();
    printf("Input str_2: \n");
    scanf("%s", str_2);
    getchar();

    while(str_1[i] == str_2[i] && str_1[i] != '\0')
    {
        i ++;
    }

    if(str_1[i] == '\0' && str_2[i] == '\0')
        printf("Equal");
    else
        printf("Not Equal");
}

// End of Ex 4.9


////////////////////////////////
//////     SEPERATOR    ////////
////////////////////////////////


// Beginning of Ex 5.1

double C_to_F(double Tc)
{
    return 9.0 / 5.0 * Tc + 32.0;
}

double F_to_C(double Tf)
{
    return (Tf - 32.0) * 5.0 / 9.0;
}

void ex5_1()
{
    char system;
    double degree;

    printf("Choose a system: C or F\n");
    scanf("%c", &system);
    getchar();

    printf("Input the degree:\n");
    scanf("%lf", &degree);
    getchar();

    if(system == 'C')
    {
        printf("%.2lf C = %.2lf F", degree, C_to_F(degree));
    }
    else if(system == 'F')
    {
        printf("%.2lf F = %.2lf C", degree, F_to_C(degree));
    }
}

// End of Ex 5.1


////////////////////////////////
//////     SEPERATOR    ////////
////////////////////////////////


// Beginning of Ex 5.4
struct poly
{
    float coef[20];
    int deg;
};

struct poly input()
{
    struct poly tpoly;
    int ind;
    printf("Enter the degree of the polynomial <20. if degree=-1 the polynomial is null\n");
    scanf("%d", &tpoly.deg);
    getchar();
    if(tpoly.deg<0)
        printf("The polynomial is null\n");
    else
    {
        for(ind = 0; ind <= tpoly.deg; ind++)
        {
            printf("Enter the coefficient%d of the polynomial\n", ind);
            scanf("%f", &tpoly.coef[ind]);
            getchar();
        }
    }
    return tpoly;

}

struct poly sum(struct poly poly1,struct poly poly2)
{
    int ind;
    struct poly poly3;
    poly3.deg = -1;
    if (poly1.deg >= poly2.deg && poly1.deg >= 0)
    {
        for(ind = 0; ind <= poly2.deg; ind++)
        {
            poly3.coef[ind] = poly1.coef[ind] + poly2.coef[ind];
        }
        for(ind = poly2.deg + 1; ind <= poly1.deg; ind++)
        {
            poly3.coef[ind] = poly1.coef[ind];
        }
        poly3.deg = poly1.deg;
    }
    else
    {
        for(ind = 0; ind <= poly1.deg; ind++)
        {
            poly3.coef[ind] = poly1.coef[ind] + poly2.coef[ind];
        }
        for(ind = poly1.deg + 1;ind <= poly2.deg; ind++)
        {
            poly3.coef[ind]=poly2.coef[ind];
        }
        poly3.deg = poly2.deg;
    }
    return poly3;

}

struct poly prodpoly(struct poly poly1, struct poly poly2)
{
    struct poly poly3;
    int ind1, ind2;
    poly3.deg = -1;
    if(poly1.deg >=0 && poly2.deg >= 0)
        poly3.deg = poly1.deg + poly2.deg;
    if(poly3.deg > 19)
    {
        printf("error:the maximal size of polynomial is reached!\n");
        poly3.deg = -1;
    }
    else if(poly3.deg >= 0)
    {
        for (ind1 = 0; ind1 <= poly3.deg; ind1++)
            poly3.coef[ind1]=0;
        for(ind1 = 0; ind1 <= poly1.deg; ind1++)
        {
            for(ind2 = 0;ind2 <= poly2.deg;ind2++)
                poly3.coef[ind1 + ind2] = poly3.coef[ind1 + ind2]
                                          + poly1.coef[ind1] * poly2.coef[ind2];
        }
    }
    return poly3;
}

float compval(struct poly poly1, float x)
{
    int ind;
    float result;
    for(ind = poly1.deg; ind >= 0; ind--)
    {
        result = result * x + poly1.coef[ind];
    }
    return result;

}

void ex5_4()
{
    struct poly p1, p2, p3, p4;
    float x, val;
    printf("Please enter the first polynomial p1\n");
    p1 = input();
    printf("Please enter the second polynomial p2\n");
    p2=  input();
    printf("Please enter the third polynomial p3\n");
    p3 = input();
    printf("Please enter the value of x in polynomial\n");
    scanf("%f", &x);
    getchar();
    p4 = sum(p1, p2);
    p4 = prodpoly(p4, p3);
    val = compval(p4, x);
    printf("The value of (p1 + p2) * p3 is %f\n", val);
    p4 = prodpoly(p1, p2);
    p4 = sum(p4, p3);
    val = compval(p4, x);
    printf("The value of (p1 * p2) + p3 is %f\n",val);
}

// End of Ex 5.4